package com.zs.backend.common.utils;

import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
public class StringUtil {
    private static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static Pattern CHINESE_PATTER = Pattern.compile("[\u4e00-\u9fa5]");

    private static Pattern versionPattern = Pattern.compile("^\\d+\\.\\d+\\.\\d+$");
    /**
     * 过滤空串
     *
     * @param str 字符串
     * @return 返回结果
     */
    public static String filterNull(String str) {
        if (str == null) {
            return "";
        } else {
            return str.trim();
        }
    }

    /**
     * 两个字符串是否相等
     *
     * @param source 原串
     * @param target 目标串
     * @return 结果
     */
    public static boolean stringEquals(String source, String target) {
        return isEmpty(source) && isEmpty(target) || !(isEmpty(source) || isEmpty(target)) && source
            .equals(target);
    }

    public static boolean isEmpty(String str) {
        return "".equals(filterNull(str));
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean containsAny(String str, String... flag) {
        if (str == null) {
            return false;
        }
        if (flag == null || flag.length == 0) {
            flag = "[-{-}-]-,".split("-");
        }
        for (String s : flag) {
            if (str.contains(s)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 是否是 JSON
     *
     * @param json 　字符串
     * @return 　返回是否是正确JSON
     */
    public static boolean isGoodJson(String json) {
        if (isNotEmpty(json)) {
            try {
                JSON.parse(json);
                return true;
            } catch (Exception e) {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 是否是 对象 JSON
     *
     * @param json 　字符串
     * @return 　返回是否是正确JSON
     */
    public static boolean isGoodObjectJson(String json) {
        if (isNotEmpty(json)) {
            try {
                JSON.parseObject(json);
                return true;
            } catch (Exception e) {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 是否是 数组 JSON
     *
     * @param json 　字符串
     * @return 　返回是否是正确JSON
     */
    public static boolean isGoodArrayJson(String json) {
        if (isNotEmpty(json)) {
            try {
                JSON.parseArray(json);
                return true;
            } catch (Exception e) {
                return false;
            }
        } else {
            return false;
        }
    }

    public static String[] listToStringArray(List<String> list) {
        if (list != null && !list.isEmpty()) {
            return list.toArray(new String[list.size()]);
        }
        return new String[0];
    }

    public static String[] setToStringArray(Set<String> set) {
        if (set != null && !set.isEmpty()) {
            return set.toArray(new String[set.size()]);
        }
        return new String[0];
    }

    public static List<String> stringArrayToList(String... strings) {
        if (strings != null && strings.length > 0) {
            return Arrays.asList(strings);
        }
        return new ArrayList<>();
    }

    public static boolean isNumber(String str) {
        //如果字符串是空串或者null直接返回false
        if (StringUtil.isEmpty(str)) {
            return false;
        }
        return str.matches("[0-9]+");

    }

    public static boolean isLetters(String str) {
        if (StringUtil.isEmpty(str)) {
            return false;
        }
        return str.matches("[a-zA-Z]+");
    }

    public static boolean isBigLetters(String str) {
        if (StringUtil.isEmpty(str)) {
            return false;
        }
        return str.matches("[A-Z]+");
    }

    public static boolean isVehicleStart(String str) {
        if (StringUtil.isEmpty(str)) {
            return false;
        }
        return str.matches("[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]");
    }

    public static boolean isNumLetters(String str) {
        if (StringUtil.isEmpty(str)) {
            return false;
        }
        return str.matches("[0-9a-zA-Z]*");
    }

    /**
     * 是否包含中文
     */
    public static boolean isContainChinese(String str) {
        return CHINESE_PATTER.matcher(str).find();
    }

    /**
     * 移除回车换行
     * @return
     */
    public static String removeCRLF(String str) {
        if (str == null) {
            return str;
        }

        return str.replaceAll("\n", " ")
            .replaceAll("\r", " ");
    }

    /**
     * 版本号比较
     *
     * @param minVersion
     * @param currentVersion
     * @return 0代表相等，1:currentVersion低于minVersion，-1: currentVersion高于minVersion
     */
    public static int compareVersion(String minVersion, String currentVersion) {
        if(isEmpty(minVersion)||isEmpty(currentVersion)){
            throw new NullPointerException();
        }
        if(!versionPattern.matcher(minVersion).matches()|| !versionPattern.matcher(currentVersion).matches()){
            throw new IllegalArgumentException();
        }

        if (minVersion.equals(currentVersion)) {
            return 0;
        }

        String[] version1Array = minVersion.split("[._]");
        String[] version2Array = currentVersion.split("[._]");
        int index = 0;
        int minLen = Math.min(version1Array.length, version2Array.length);
        long diff = 0;

        while (index < minLen
            && (diff = Long.parseLong(version1Array[index])
            - Long.parseLong(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            for (int i = index; i < version1Array.length; i++) {
                if (Long.parseLong(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Long.parseLong(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }

    public static String encryptPhoneNumber(String phoneNumber) {
        if (phoneNumber == null || phoneNumber.length() != 11) {
            //throw new IllegalArgumentException("手机号格式不正确");
            return phoneNumber;
        }

        // 获取手机号的前三位和后四位
        String prefix = phoneNumber.substring(0, 3);
        String postfix = phoneNumber.substring(7);

        // 对手机号中间四位进行加密，用 "*" 替换
        String middle = "****";

        // 拼接加密后的手机号
        return prefix + middle + postfix;
    }

    public static String encryptIdCardNumber(String idCardNumber) {
        if (idCardNumber == null || idCardNumber.length() != 18) {
            //throw new IllegalArgumentException("身份证号码格式不正确");
            return idCardNumber;
        }

        // 获取身份证号码的前6位和后4位
        String prefix = idCardNumber.substring(0, 6);
        String postfix = idCardNumber.substring(14);

        // 对身份证号码中间的8位生日码进行加密，用 "*" 替换
        String middle = "********";

        // 拼接加密后的身份证号码
        return prefix + middle + postfix;
    }

}